﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using SimpleFileDataAndViewModels;
using BrainTechLLC;
using System.IO;

namespace SimpleImageApplication
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        DirectoriesToExamine directories = new DirectoriesToExamine(new string[] { @"C:\Owen\Crawled\H" });
        HasBytes fileImage = new HasBytes()
        {
            FileName = @"C:\owen\crawled\H\gen11.jpg"
        };

        public MainWindow()
        {
            InitializeComponent();
        }

        private void mnuExit_Click(object sender, RoutedEventArgs e)
        {
            Application.Current.Shutdown();
        }

        private void mnuTest_Click(object sender, RoutedEventArgs e)
        {
            ImageProvider provider = new ImageProvider(fileImage);
            imageViewer.IImageSourceProvider = provider;
        }

        private void mnuTest2_Click(object sender, RoutedEventArgs e)
        {
            ImageProviderCollection collection = new ImageProviderCollection();
            ThumbnailImageProvider provider = new ThumbnailImageProvider(fileImage)
            {
                 TargetBitmapWidth = 100,
                 TargetBitmapHeight = 80,
            };
            imageViewer.IImageSourceProvider = provider;

            for (int n = 0; n < directories.Count; n++)
            {
                DirectoryToExamine dir = directories[n];
                string[] files = Directory.GetFiles(dir.Directory);

                for (int i = 0; i < files.Length; i++)
                {
                    if (files[i].EndsWith(".jpg", StringComparison.OrdinalIgnoreCase))
                    {
                        HasBytes fi = new HasBytes() { FileName = files[i] };
                        ThumbnailImageProvider thumb = new ThumbnailImageProvider(fi)
                        {
                            TargetBitmapWidth = 100,
                            TargetBitmapHeight = 80,
                        };
                        collection.Add(thumb);
                    }
                }
            }
            thumbViewer.ViewModel = collection;
        }

        private void Window_Drop(object sender, DragEventArgs e)
        {
            if (e.Data != null && e.Data.GetDataPresent(System.Windows.DataFormats.FileDrop))
            {
                string[] fileNames = (string[])e.Data.GetData(System.Windows.DataFormats.FileDrop);
                fileImage.FileName = fileNames[0];
                fileImage.ClearAll();
                fileImage.RaisePropertyChanged(null);
            }
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {            
            directories.DataProvider = App.DataProvider;            
        }
    }
}
